﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

// http://www.codeproject.com/KB/user-controls/popupcalendarcontrol.aspx
public partial class PopupCalendar : System.Web.UI.UserControl
{
    [Browsable(true),
    Category("Behavior"),
    Description("Gets or sets the selected date")]
    public DateTime Date
    {
        get { return DateTime.Parse(this.tbDate.Text); }
        set { this.tbDate.Text = value.ToShortDateString(); }
    }

    [Browsable(true),
    Category("Behavior"),
    Description("Gets or sets whether or not the control show allow dates in the past")]
    public bool OnlyFutureDates
    {
        get;
        set;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if( string.IsNullOrEmpty(this.tbDate.Text))
                this.tbDate.Text = System.DateTime.Now.ToShortDateString();

            this.pnlCalendar.Attributes.Add("style",
                 "DISPLAY: none; POSITION: absolute");
        }
        else
        {
            string curEventTarget = Page.Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrEmpty(curEventTarget))
            {
                int cutOffIdx = curEventTarget.LastIndexOf("$");
                string clientIdStr = curEventTarget.Substring(0, cutOffIdx);
                clientIdStr = clientIdStr.Replace("$", "_");
                if (clientIdStr != this.ClientID)
                {
                    pnlCalendar.Attributes.Add("style", "DISPLAY: none; POSITION: absolute");
                }
                else
                {
                    pnlCalendar.Attributes.Add("style", "POSITION: absolute");
                }
            }
        }

        Page.ClientScript.RegisterClientScriptBlock(typeof(PopupCalendar), "Script_Panel" + this.ClientID, 
            "<script> function On" + this.ClientID + "Click() { alert('click'); if(" + this.ClientID + 
            "_pnlCalendar.style.display == \"none\") {alert('none->blank');" + this.ClientID +
            "_pnlCalendar.style.display = \"\";} else {alert('blank->none');" + this.ClientID + 
            "_pnlCalendar.style.display = \"none\";} } </script>");

        this.Button1.Attributes.Add("OnClick", "On" + this.ClientID + "Click()");
    }
}
